<template>
  <div class="sm-wpu-workpackageUnpack" >
    <TableTitle>
      <slot>
        <p>
          扫码操作
        </p>
      </slot>
    </TableTitle>
    <TopSearch class="topSearch-box" ref="topSearch">
      <slot>
        <li>
          <el-button size="mini" type="primary" @click="callUnpack">执行拆包叫箱</el-button>
        </li>
     <!--   <li>
          <el-button size="mini" type="primary" @click="gzbComeBtn">工作包出库</el-button>
        </li>-->
        <li>
          <el-button size="mini" type="primary" @click="gzbScan">工作包扫码</el-button>
        </li>
        <li>
          <el-button size="mini" type="primary" @click="gzbBackBtn">工作包回库</el-button>
        </li>
      <!--  <li>
          <el-button size="mini" type="success" @click="hcxComeBtn">缓存箱出库</el-button>
        </li>-->
        <li>
          <el-button size="mini" type="success" @click="hcxScan">缓存箱扫码</el-button>
        </li>
        <li>
          <el-button size="mini" type="success" @click="hcxBoxBackBtn">缓存箱回库</el-button>
        </li>
        <li>
          <el-button size="mini" type="success" @click="saveBtn">拆包保存</el-button>
        </li>
        <li >
          <p>
            工作包条码：<span>{{gzbTM}}</span>
          </p>
          <p>
            缓存箱条码：<span>{{hcxTM}}</span>
          </p>
        </li>
      </slot>
    </TopSearch>
  <!--  <TableTitle>
      <slot>
        <p>
          已分拣任务列表
        </p>
      </slot>
    </TableTitle>
    <TopTable></TopTable>-->
    <TableTitle>
      <slot>
        <p>
          分拣详情
        </p>
      </slot>
    </TableTitle>
    <unpackInfor></unpackInfor>
   <!--<LastGoods></LastGoods>--> 
  </div>
</template>
<script>
import TopSearch from "../../../../components/TopSearch.vue";
import TableTitle from "../../../../components/TableTitle.vue";
import TopTable from "./components/TopTable.vue";
import MidTable from "./components/MidTable.vue";
import unpackInfor from "./components/unpackInfo.vue";
import LastGoods from "./components/LastGoods.vue"
import { smSmanHcxScan,smSmanGzbScan,smSmanCsHcxOut,smSmanCsHcxBack,workBoxSmOut,workBoxSmBack,workUnpackSave,unpackCallBox } from "@/utils/api.js"
export default {
  name: "WorkpackageUnpack",
  components: {
    TopSearch,
    TopTable,
    MidTable,
    TableTitle,
    LastGoods,
    unpackInfor
  },
  data(){
    return{
      hcxGjbInfo:[],
      // 缓存箱出库按钮信息list
      hcxList:[],
      indexTm:'',
      hcxTM:'',
      gzbTM:'',
      gzbListInfo:[],
      hcxBoxBack:'',
      hcxBoxCome:'',
      doUnpack:[],
      toolResult:[]
    }
  },
  methods:{
    // 统一扫码判断接口方法
    scanning(){
      if(this.indexTm.length == 12){
        let indexTmList = [...this.indexTm]
        if(indexTmList[0] == 1){
          // 缓存箱扫码
          this.$options.methods.hcxScan.bind(this)()
        }else if(indexTmList[0] == 2){
          // 工作包扫码
          this.$options.methods.gzbScan.bind(this)()
        }
      }else if(this.indexTm.length == 4){
          if(this.indexTm == 9002){
          // 缓存箱回库
          this.$options.methods.hcxBoxBackBtn.bind(this)()
        }else if(this.indexTm == 9004){
          // 工作包回库
          this.$options.methods.gzbBackBtn.bind(this)()
        }else if(this.indexTm == 9005){
          // 分拣保存
          this.$options.methods.saveBtn.bind(this)()
        }else {
        this.$Alert({
            title: "失败",
            message: '条码有误！',
            type: "error",
          })
        } 
      }
      
    },
    // 缓存箱出库 -----更换对的接口就行 格式已经写好
    hcxComeBtn(tiaoma){
      smSmanCsHcxOut({
          // msTaskAoIds:this.hcxBoxCome
          aoBarcode:this.indexTm
        }).then((res)=>{
          console.log('缓存箱出库',res)
          if(res.data.retCode == 200){
            this.$Alert({
              title: "成功",
              message: '缓存箱出库命令已下发！',
              type: "success",
            });
            this.aoNumber = tiaoma
            // 出库成功聚焦条码框 并清空
            this.$refs.topSearch.makeTmNull()
            this.$refs.topSearch.openScan()
          }else {
            this.$Alert({
              title: "失败",
              message: '无法下发缓存箱出库命令！',
              type: "error",
            });
          }
        })
    },
    // 缓存箱扫码
    hcxScan(){
      this.hcxTM = this.indexTm;
    },
    // 缓存箱回库
    hcxBoxBackBtn(){
      this.hcxBackTm = this.hcxTM
      smSmanCsHcxBack({
          workbinCode:this.hcxBackTm
        }).then((res)=>{
          console.log('缓存箱回库',res)
          if(res.data.retCode == 200){
            this.$Alert({
              title: "成功",
              message: '缓存箱回库命令已下发！',
              type: "success",
            });
            document.querySelector('.sm-sman-rightInfo').style.display = 'none'
            this.hcxTM = ''
            // 回库成功清空条码框并聚焦
            this.$refs.topSearch.makeTmNull()
            this.$refs.topSearch.openScan()
          }else {
            this.$Alert({
              title: "失败",
              message: '无法下发缓存箱回库命令！',
              type: "error",
            });
          }
        })
    },
    // 工作包出库  
    gzbComeBtn(tiaoma){
      workBoxSmOut({
        agv:'',
        // msTaskAoIds:this.gzbCome
        aoBarcode:this.indexTm
      }).then((res)=>{
        console.log('工作包出库',res)
        if(res.data.retCode == 200){
          this.aoNumber = tiaoma
          this.$Alert({
            title: "成功",
            message: '工作包出库命令已下发！',
            type: "success",
          });
          // 出库成功聚焦条码框 并清空
          this.$refs.topSearch.makeTmNull()
          this.$refs.topSearch.openScan()
        }else {
          this.$Alert({
            title: "失败",
            message: '无法下发工作包出库命令！',
            type: "error",
          });
        }
      })
    },
    // 工作包扫码
    gzbScan(){
      this.gzbTM = this.indexTm;
    },
    // 工作包回库
    gzbBackBtn(){
      this.gzbBackTm = this.gzbTM
      workBoxSmBack({
          palletCode:this.gzbBackTm
        }).then((res)=>{
          console.log('工作包回库',res)
          if(res.data.retCode == 200){
            this.$Alert({
              title: "成功",
              message: '工作包回库命令已下发！',
              type: "success",
            });
            document.querySelector('.sm-sman-leftInfo').style.display = 'none'
            this.gzbTM = ''
            // 回库成功清空条码框并聚焦
            this.$refs.topSearch.makeTmNull()
            this.$refs.topSearch.openScan()
          }else {
            this.$Alert({
              title: "失败",
              message: '无法下发工作包回库命令！',
              type: "error",
            });
          }
        })
    },
    //拆包叫箱
    callUnpack(){
      unpackCallBox({
        unpackInfoList:this.doUnpack
      }).then((res)=>{
        console.log('分拣叫箱',res)
        if(res.data.retCode=200){
          this.$Alert({
            title: "成功",
            message: '叫箱成功',
            type: "success",
          });
        }else{
          this.$Alert({
            title: "失败",
            message: '叫箱失败',
            type: "error",
          });
        }
      })
    },
    // 拆包保存
    saveBtn(){
      if(this.doUnpack.length!=1){
        this.$Alert({ title: "失败",message: '请选择一条数据进行拆包保存操作', type: "error",});
        return ;
      }
      workUnpackSave({
        workbinCode:this.hcxTM,
        toolCode:this.gzbTM,
        unpackDto:this.doUnpack
      }).then((res)=>{
        console.log('保存',res)
        if(res.data.retCode == 200) {
          this.$Alert({
            title: "成功",
            message: '保存成功',
            type: "success",
          });
          document.querySelector('.sm-sman-rightInfo').style.display = 'none'
          document.querySelector('.sm-sman-leftInfo').style.display = 'none'
          this.$refs.topSearch.makeTmNull()
          this.$refs.topSearch.openScan()
        }else {
          this.$Alert({
            title: "失败",
            message: '保存失败',
            type: "error",
          });
        }
      })
    }
  },
  mounted(){
    this.$bus.$on('saoma',(e)=>{
      this.indexTm = e
    })
    // 接收midTable勾选信息传给首页 点击缓存箱扫码和工具包扫码
    // this.$bus.$on('smSmanHcxGjbInfo',(e)=>{
    //   this.hcxGjbInfo = e
    //   // console.log(this.hcxGjbInfo)
    //   for(let i=0;i<e.length;i++){
    //     this.hcxList.push(e[i].itemCode)
    //   }
    // })
    this.$bus.$on('WuGzbListInfo',(e)=>{
      this.gzbListInfo = e
      console.log(e,3333)
    })
    // 传数据给 hcx出库 hcx回库按钮
    this.$bus.$on('smWuHcxInfo',(e)=>{
      this.hcxBoxBack = e
      this.hcxBoxCome = e
    })
    // 物料确认完 回车 聚焦index input
    this.$bus.$on('smWuMatiToBtninput',(e)=>{
      // 回库成功清空条码框并聚焦
      this.$refs.topSearch.makeTmNull()
      this.$refs.topSearch.openScan()
    })
    this.$bus.$on('getDoUnpackInfo',(e)=>{
      this.doUnpack = e
      console.log("e",e)
    })
  }
};
</script>
<style scoped>
.el-button--success {
  background: #29bacd;
  border-color: #29bacd;
}
.el-button--success:hover {
  background: #23abbd;
  border-color: #23abbd;
}
.topSearch-box {
  height: 80px;
}
</style>